<?php
/**
 * Introduction: 专题逻辑处理
 * @author: 杨陈鹏
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class SpecialLogic
{

    static public $model,$model_comment;

    public function __construct()
    {

        self::$model = new Model\SpecialModel();

        self::$model_comment = new Model\SpeciaCommentModel();

    }
    /**
     * Introduction: 创建数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function create($data)
    {

        if(!$data['title'])
            return ['code' => 300, 'msg' => '标题不能为空!'];

        if(!$data['description'])
            return ['code' => 300, 'msg' => '描述不能为空!'];

        if(!$data['content'])
            return ['code' => 300, 'msg' => '内容不能为空!'];

        if(!$data['thumb'])
            return ['code' => 300, 'msg' => '请上传缩略图!'];

        $datas['title'] = $data['title'];
        $datas['description'] = $data['description'];
        $datas['content'] = $data['content'];
        $datas['thumb'] = $data['thumb'];
        $datas['create_time'] = NOW_TIME;

        if(self::$model->add($datas))
            return ['code' => 200, 'msg' => '发布成功!'];

        return ['code' => 300, 'msg' => '发布失败!'];

    }
    /**
     * Introduction: 修改数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function update($where,$data)
    {

        if(!$data['title'])
            return ['code' => 300, 'msg' => '标题不能为空!'];

        if(!$data['description'])
            return ['code' => 300, 'msg' => '描述不能为空!'];

        if(!$data['content'])
            return ['code' => 300, 'msg' => '内容不能为空!'];

        if(!$data['thumb'])
            return ['code' => 300, 'msg' => '请上传缩略图!'];

        if(!$where)
            return ['code' => 300, 'msg' => '缺少条件!'];

        $datas['title'] = $data['title'];
        $datas['description'] = $data['description'];
        $datas['content'] = $data['content'];
        $datas['thumb'] = $data['thumb'];
        $datas['create_time'] = NOW_TIME;

        $result = self::$model->where($where)->save($datas);

        if($result !== false)
            return ['code' => 200, 'msg' => '更新成功!'];

        return ['code' => 300, 'msg' => '更新失败!'];

    }

    /**
     * Introduction: 获取数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getData($where,$fields = '*',$order = 'create_time desc,likes desc',$page = 1,$nums = 20)
    {

        $data = self::$model
                        ->where($where)
                        ->field($fields)
                        ->order($order)
                        ->page($page,$nums)
                        ->select();

        $total = self::$model
                        ->where($where)
                        ->count();


        foreach($data as $k => $v)
        {

            $data[$k]['create_time'] = date('Y-m-d H:i:s',$data[$k]['create_time']);

        }

        return ['code' => 200, 'data' => $data,'total' => $total];

    }

    /**
     * Introduction: 获取一条数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getOneData($id,$fields = '*',$getComment = true)
    {


        if(!intval($id))
            return ['code' => 300, 'msg' => '缺少专题信息!'];

        $data = self::$model
                        ->where(['id' => $id])
                        ->field($fields)
                        ->find();

        if($getComment){
            $comment_info = self::$model_comment
                ->where(['special_id' => $data['id']])
                ->order('create_time desc')
                ->select();

            $member_logic = D('Common/Member','Logic');

            foreach($comment_info as $k => $v)
            {

                $user_info = $member_logic::$model
                    ->where(['uid' => $v['uid']])
                    ->field('uid,username,nicname,avatar')
                    ->find();

                $comment_info[$k]['username'] = $user_info['username'];
                $comment_info[$k]['nicname'] = $user_info['nicname'];
                $comment_info[$k]['avatar'] = $user_info['avatar'];
                $comment_info[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);


            }

            $data['comment_info'] = $comment_info;

        }



        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

        return ['code' => 200, 'data' => $data];

    }
    /**
     * Introduction: 获取一条数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function like($id)
    {


        if(!intval($id))
            return ['code' => 300, 'msg' => '缺少专题信息!'];

        if(self::$model->where(['id' => $id])->setInc('likes')){

            $likes = self::$model->where(['id' => $id])->getField('likes');

            return ['code' => 200, 'msg' => '操作成功!','data' =>['likes'=>$likes]];

        }

        return ['code' => 300, 'msg' => '操作失败!'];

    }
    /**
     * Introduction: 设置专题浏览次数
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function views($id)
    {


        if(!intval($id))
            return ['code' => 300, 'msg' => '缺少专题信息!'];

        if(self::$model->where(['id' => $id])->setInc('views')){

            $likes = self::$model->where(['id' => $id])->getField('likes');

            return ['code' => 200, 'msg' => '操作成功!','data' =>['likes'=>$likes]];

        }

        return ['code' => 300, 'msg' => '操作失败!'];

    }
    /**
     * Introduction: 专题评论
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function comment($data)
    {

        if(!intval($data['uid']))
            return ['code' => 300, 'msg' => '请登录!'];

        if(!intval($data['special_id']))
            return ['code' => 300, 'msg' => '缺少专题信息!'];

        $datas['uid'] = $data['uid'];
        $datas['special_id'] = $data['special_id'];
        $datas['create_time'] = NOW_TIME;
        $datas['content'] = $data['content'];

        //return self::$model_comment->fetchSql(true)->add($datas);

        if(self::$model_comment->add($datas)){

            //评论成功评论数量 +1

            self::$model->where(['id' => $data['special_id']])->setInc('comment');

            return ['code' => 200, 'msg' => '评论成功!'];

        }

        return ['code' => 300, 'msg' => '评论失败!'];

    }

    /**
     * Introduction: 获取专题评论信息
     * @author: 王龙
     * @email: wanglong@cdlinglu.com
     */
    static public function getComment($where,$fields = '*',$order = 'create_time desc',$page = 1,$nums = 20)
    {

        $data = self::$model_comment
            ->where($where)
            ->field($fields)
            ->order($order)
            ->page($page,$nums)
            ->select();

        $total = self::$model_comment
            ->where($where)
            ->count();

        if(!$data)
            return ['code' => 200, 'msg' => '暂无数据!'];

        $user_logic = D('Common/Member','Logic');

        foreach($data as $k => $v)
        {

            $data[$k]['create_time'] = date('Y-m-d H:i:s',$data[$k]['create_time']);

            //获取用户信息
            $user_info = $user_logic::$model
                ->where(['uid' => $v['uid']])
                ->field('avatar,username,nicname')
                ->find();

            $data[$k]['avatar'] = $user_info['avatar'];
            $data[$k]['username'] = $user_info['username'];
            $data[$k]['nicname'] = $user_info['nicname'];
        }

        return ['code' => 200, 'data' => $data,'total' => $total];

    }
}